<template>
  <div class="my-activities">
    <!-- 贡献统计卡片 -->
    <el-row :gutter="20" class="contribution-cards">
      <el-col :span="8">
        <el-card class="contribution-card">
          <template #header>
            <div class="card-header">
              <el-icon><Calendar /></el-icon>
              <span>参与活动</span>
            </div>
          </template>
          <div class="card-content">
            <div class="number">{{ contribution.totalActivities }}</div>
            <div class="label">总参与次数</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="contribution-card">
          <template #header>
            <div class="card-header">
              <el-icon><Star /></el-icon>
              <span>信用积分</span>
            </div>
          </template>
          <div class="card-content">
            <div class="number">{{ contribution.totalCreditPoints }}</div>
            <div class="label">累计获得积分</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="contribution-card">
          <template #header>
            <div class="card-header">
              <el-icon><Trophy /></el-icon>
              <span>环保贡献</span>
            </div>
          </template>
          <div class="card-content">
            <div class="number">{{ contribution.totalContribution }}</div>
            <div class="label">环保贡献值</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 活动记录 -->
    <el-card class="activity-records">
      <template #header>
        <div class="card-header">
          <span>活动记录</span>
          <el-radio-group v-model="activeTab" size="small">
            <el-radio-button label="all">全部</el-radio-button>
            <el-radio-button label="upcoming">即将开始</el-radio-button>
            <el-radio-button label="completed">已完成</el-radio-button>
          </el-radio-group>
        </div>
      </template>

      <el-table :data="activities" style="width: 100%">
        <el-table-column prop="title" label="活动名称" min-width="200">
          <template #default="{ row }">
            <div class="activity-title">
              <el-image :src="row.image" class="activity-image" />
              <span>{{ row.title }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="活动类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getActivityTypeTag(row.type)">{{ row.type }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="活动时间" width="180" />
        <el-table-column prop="location" label="活动地点" width="150" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">{{ getStatusText(row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="creditPoints" label="获得积分" width="100">
          <template #default="{ row }">
            <span v-if="row.creditPoints" class="credit-points">+{{ row.creditPoints }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button
              link
              type="primary"
              @click="viewDetail(row.id)"
            >
              查看详情
            </el-button>
            <el-button
              v-if="row.status === 'upcoming'"
              link
              type="danger"
              @click="cancelActivity(row.id)"
            >
              取消报名
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Calendar,
  Star,
  Trophy
} from '@element-plus/icons-vue'
import { getUserActivities, getUserContribution, cancelActivity } from '@/api/social'

const router = useRouter()

// 贡献统计
const contribution = ref({
  totalActivities: 0,
  totalCreditPoints: 0,
  totalContribution: 0
})

// 活动列表
const activities = ref([])
const activeTab = ref('all')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 加载贡献统计
const loadContribution = async () => {
  try {
    const response = await getUserContribution()
    contribution.value = response.data
  } catch (error) {
    ElMessage.error('获取贡献统计失败')
  }
}

// 加载活动列表
const loadActivities = async () => {
  try {
    const params = {
      status: activeTab.value === 'all' ? '' : activeTab.value,
      page: currentPage.value,
      size: pageSize.value
    }
    const response = await getUserActivities(params)
    activities.value = response.data.list
    total.value = response.data.total
  } catch (error) {
    ElMessage.error('获取活动列表失败')
  }
}

// 活动类型标签
const getActivityTypeTag = (type) => {
  const typeMap = {
    'recycling': 'success',
    'water': 'primary',
    'welfare': 'warning'
  }
  return typeMap[type] || 'info'
}

// 状态标签
const getStatusType = (status) => {
  const statusMap = {
    'upcoming': 'warning',
    'completed': 'success',
    'cancelled': 'info'
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    'upcoming': '即将开始',
    'completed': '已完成',
    'cancelled': '已取消'
  }
  return statusMap[status] || status
}

// 查看详情
const viewDetail = (id) => {
  router.push(`/social/activities/${id}`)
}

// 取消报名
const handleCancel = async (id) => {
  try {
    await ElMessageBox.confirm('确定要取消报名吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    await cancelActivity(id)
    ElMessage.success('取消报名成功')
    loadActivities()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('取消报名失败')
    }
  }
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  loadActivities()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  loadActivities()
}

// 监听标签切换
watch(activeTab, () => {
  currentPage.value = 1
  loadActivities()
})

onMounted(() => {
  loadContribution()
  loadActivities()
})
</script>

<style lang="scss" scoped>
.my-activities {
  padding: 20px;

  .contribution-cards {
    margin-bottom: 20px;

    .contribution-card {
      .card-header {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 16px;
        color: #303133;

        .el-icon {
          font-size: 20px;
        }
      }

      .card-content {
        text-align: center;
        padding: 20px 0;

        .number {
          font-size: 36px;
          font-weight: bold;
          color: #409EFF;
          margin-bottom: 10px;
        }

        .label {
          color: #909399;
          font-size: 14px;
        }
      }
    }
  }

  .activity-records {
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .activity-title {
      display: flex;
      align-items: center;
      gap: 10px;

      .activity-image {
        width: 40px;
        height: 40px;
        border-radius: 4px;
      }
    }

    .credit-points {
      color: #67C23A;
      font-weight: bold;
    }
  }

  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style> 